package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
//@Transactional mongodb事务必须复制集方式
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private StrategyRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);//防止游标
        repository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQueryObject qo) {
        //查询的条件对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(new Criteria().where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())) {
            query.addCriteria(new Criteria().where("themeId").is(qo.getThemeId()));
        }
        //拼接条件
        long count = template.count(query, Strategy.class);//计算一共有多少条数据 query 为无条件 totalCount  集合MOngoDb
        if (count == 0) {
            return Page.empty();//空的页面对象
        }
        //分页加排序---->条件拼接
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        //把条件放入查询对象中
        query.with(pageRequest);
        //查询当前结果集template.find
        List<Strategy> list = template.find(query, Strategy.class);
        //PageImpl等价于之前的PageResult
        return new PageImpl<>(list, pageRequest, count);//PageResult原生的  pageRequest当前页  多少条数
        //PageHadler  分页插件的
    }

    public void saveOrUpdate(Strategy strategyTheme) {
        strategyTheme.setCreateTime(new Date());
        strategyTheme.setThemeName(strategyThemeService.get(strategyTheme.getThemeId()).getName());
        strategyTheme.setCatalogName(strategyCatalogService.get(strategyTheme.getCatalogId()).getName());
        strategyTheme.setDestName(strategyCatalogService.get(strategyTheme.getCatalogId()).getDestName());
        strategyTheme.setDestId(strategyCatalogService.get(strategyTheme.getCatalogId()).getDestId());
        if (StringUtils.hasLength(strategyTheme.getId())) {
            repository.save(strategyTheme);
        } else {
            strategyTheme.setId(null);
            template.insert(strategyTheme);
        }
    }

    @Override
    public void changeState(String id, Integer state) {
        Strategy strategy = this.get(id);
        strategy.setState(state);
        repository.save(strategy);
    }

    @Override
    public List<Strategy> getByDestId(String destId) {
        return repository.findByDestId(destId);
    }

    @Override
    public List<Strategy> getByCatalogName(String name) {
        return repository.findByCatalogName(name);
    }

    @Override
    public List<Strategy> viewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        return repository.findByDestId(destId, pageable);
    }
}